#include "REG51F.H"

/*  P3  */
sbit MODE = P3^2;
sbit SELECT   = P3^3;
sbit CHANGE = P3^4;

/*  P1  */
sbit MAIN_RESET_RED = P1^3;
sbit SH_CP_RED   = P1^2;
sbit ST_CP_RED = P1^1;
sbit DATA_RED   = P1^0;
/*  P1  */
sbit MAIN_RESET_GRE = P1^7;
sbit SH_CP_GRE   = P1^6;
sbit ST_CP_GRE = P1^5;
sbit DATA_GRE   = P1^4;

unsigned char count_over = 0,count_over_1 = 0, second_now = 0, am_pm = 0;
unsigned char second = 0, minus = 0, hour = 0, day = 10, month = 6, set_hour, set_minus, set_time_ok = 0, check_out_time = 0;
unsigned char hour_alert = 0, minus_alert = 0, stop_larm = 0;
unsigned char minus_sport = 0, second_sport = 0, percent_sport = 0, stop = 0, check_stop = 0, m_sport, s_sport, p_sport; 
unsigned int year = 2013;
unsigned char data_base[48];
unsigned char set_larm = 0,check_day = 0;
unsigned char mod = 0,sel = 0,inc = 0;

unsigned char code DATA_OFFSET[]=
{
	0xc3,0xbd,0x7e,0x7e,0x81,0xc3,0xff,  //num 0
	0xee,0xde,0x80,0x00,0xfe,0xfe,0xff, 	//num 1
	0xde,0xbc,0x7a,0x76,0x86,0xce,0xff,  //num 2
	0xbd,0x7e,0x6e,0x6e,0x00,0x91,0xff,	//num 3
	0xe3,0xdb,0xbb,0x00,0x00,0xfb,0xff,	//num 4
	0x0d,0x0e,0x6e,0x6e,0x60,0x71,0xff,  //num 5
	0x81,0x00,0x6e,0x6e,0x60,0xb1,0xff,	//num 6
	0x7c,0x78,0x73,0x67,0x0f,0x1f,0xff,	//num 7
	0x91,0x6e,0x6e,0x6e,0x00,0x91,0xff,	//num 8
	0x9d,0x6e,0x6e,0x6e,0x00,0x81,0xff,	//num 9, pos 69
	0x99,0x99,0xff,	//dau : , pos 72
	0xf7,0xf7,0xff,	//dau - , pos 75
	0xff,0xe0,0xd7,0xb7,0xb7,0xc0,0xe0,0xff,	// a key   pos 83
	0xff,0x80,0xbb,0xbb,0xbb,0x93,0xc7,0xff, //p key	pos 91
	0xff,0x00,0x00,0x7e,0x7e,0x81,0xc3,0xff,	//D key  pos 99
	0xc3,0xbd,0x7e,0x7e,0x81,0xc3,0xff,  //O key pos 106
	0x00,0x00,0xe7,0xdb,0xbd,0x7e,0xff,  //K key pos 113
	0x00,0x9f,0xcf,0xe7,0x00,0x00,0xff,  //N key pos 120
	0xdb,0x99,0xff,    // dau AM   pos 123
	0x99,0xbd,0xff,	   // dau PM
};

void set_Sport(){
	unsigned char i,z, col, count = 0;
	for (i = 0;i<6;i++){
		for (z=0;z<8;z++){
			col = i*8+z;
			if (col <= 6){
				data_base[col] = DATA_OFFSET[(minus_sport/10)*7+count];
				count++; 
			}
			else if (col <= 13){
				data_base[col] = DATA_OFFSET[(minus_sport%10)*7+count];
				count++; 
			}
			else if (col <= 16){
				data_base[col] = DATA_OFFSET[70+count];
				count++; 
			}
			else if (col <= 23){
				data_base[col] = DATA_OFFSET[(second_sport/10)*7+count];
				count++; 
			}
			else if (col <= 30){
				data_base[col] = DATA_OFFSET[(second_sport%10)*7+count];
				count++;
			}
			else if (col <= 33){
				data_base[col] = DATA_OFFSET[70+count];
				count++;
			}
			else if (col <= 40){
				data_base[col] = DATA_OFFSET[(percent_sport/10)*7+count];
				count++;
			}
			else if (col <= 47){
				data_base[col] = DATA_OFFSET[(percent_sport%10)*7+count];
				count++;
			}
			
			if (col == 6 || col == 13 || col == 16 || col == 23 || col == 30 || col == 33 || col == 40 || col == 47){
				count = 0;
			}
		}		  //  1 matrix.
	}
}

void set_Sport_Mode(){
	unsigned char i,z, col, count = 0;
	for (i = 0;i<6;i++){
		for (z=0;z<8;z++){
			col = i*8+z;
			if (col <= 6){
				data_base[col] = DATA_OFFSET[(m_sport/10)*7+count];
				count++; 
			}
			else if (col <= 13){
				data_base[col] = DATA_OFFSET[(m_sport%10)*7+count];
				count++; 
			}
			else if (col <= 16){
				data_base[col] = DATA_OFFSET[70+count];
				count++; 
			}
			else if (col <= 23){
				data_base[col] = DATA_OFFSET[(s_sport/10)*7+count];
				count++; 
			}
			else if (col <= 30){
				data_base[col] = DATA_OFFSET[(s_sport%10)*7+count];
				count++;
			}
			else if (col <= 33){
				data_base[col] = DATA_OFFSET[70+count];
				count++;
			}
			else if (col <= 40){
				data_base[col] = DATA_OFFSET[(p_sport/10)*7+count];
				count++;
			}
			else if (col <= 47){
				data_base[col] = DATA_OFFSET[(p_sport%10)*7+count];
				count++;
			}
			
			if (col == 6 || col == 13 || col == 16 || col == 23 || col == 30 || col == 33 || col == 40 || col == 47){
				count = 0;
			}
		}		  //  1 matrix.
	}
}
void update_Matrix (void) {
	unsigned col;
	for (col = 0;col < 6; col++){
		data_base[col] = DATA_OFFSET[((am_pm == 1 ? (hour > 12 ? hour - 12: hour): hour)/10)*7 + col];
		data_base[col+7] = DATA_OFFSET[((am_pm == 1 ? (hour > 12 ? hour - 12: hour): hour)%10)*7 + col];
		data_base[col+17] = DATA_OFFSET[(minus/10)*7 + col];
		data_base[col+24] = DATA_OFFSET[(minus%10)*7 + col];
		data_base[col+34] = DATA_OFFSET[(second/10)*7 + col];
		data_base[col+41] = DATA_OFFSET[(second%10)*7 + col];
	}
	data_base[14] = DATA_OFFSET[(am_pm == 1 ? (hour > 12 ? 124: 121): 70)];
	data_base[15] = DATA_OFFSET[(am_pm == 1 ? (hour > 12 ? 125: 122): 71)];
	data_base[31] = DATA_OFFSET[70];
	data_base[32] = DATA_OFFSET[71];
}

void Date(){
	unsigned col;
	for (col =0;col < 6; col++){
		data_base[col] = DATA_OFFSET[(day/10)*7 + col];
		data_base[col+7] = DATA_OFFSET[(day%10)*7 + col];
		data_base[col+17] = DATA_OFFSET[(month/10)*7 + col];
		data_base[col+24] = DATA_OFFSET[(month%10)*7 + col];
		data_base[col+34] = DATA_OFFSET[(year%100)/10*7 + col];
		data_base[col+41] = DATA_OFFSET[(year%10)*7 + col];
	}
}

void larm_Update (void) {
	unsigned col;
	for (col = 0;col < 6; col++){
		data_base[col] = DATA_OFFSET[((am_pm == 1 ? (hour_alert > 12 ? hour_alert - 12: hour_alert): hour_alert)/10)*7 + col];
		data_base[col+7] = DATA_OFFSET[((am_pm == 1 ? (hour_alert > 12 ? hour_alert - 12: hour_alert): hour_alert)%10)*7 + col];
		data_base[col+17] = DATA_OFFSET[(minus_alert/10)*7 + col];
		data_base[col+24] = DATA_OFFSET[(minus_alert%10)*7 + col];
		data_base[col+34] = DATA_OFFSET[((set_larm == 0) ? 114 : 100)+ col];
		data_base[col+41] = DATA_OFFSET[((set_larm == 0) ?  100: 107) + col];
	}
	data_base[14] = DATA_OFFSET[(am_pm == 1 ? (hour_alert > 12 ? 124: 121): 70)];
	data_base[15] = DATA_OFFSET[(am_pm == 1 ? (hour_alert > 12 ? 125: 122): 71)];
}

void set_Time_Update (void) {
	unsigned col;
	for (col = 0;col < 6; col++){
		data_base[col] = DATA_OFFSET[((am_pm == 1 ? (set_hour > 12 ? set_hour - 12: set_hour): set_hour)/10)*7 + col];
		data_base[col+7] = DATA_OFFSET[((am_pm == 1 ? (set_hour > 12 ? set_hour - 12: set_hour): set_hour)%10)*7 + col];
		data_base[col+17] = DATA_OFFSET[(set_minus/10)*7 + col];
		data_base[col+24] = DATA_OFFSET[(set_minus%10)*7 + col];
		data_base[col+34] = DATA_OFFSET[((set_time_ok == 0) ? 114 : 100)+ col];
		data_base[col+41] = DATA_OFFSET[((set_time_ok == 0) ?  100: 107) + col];
	}
	data_base[14] = DATA_OFFSET[(am_pm == 1 ? (set_hour > 12 ? 124: 121): 70)];
	data_base[15] = DATA_OFFSET[(am_pm == 1 ? (set_hour > 12 ? 125: 122): 71)];
}

void display_Time_Alarm(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp;

	if ((second % 2 == 1) || (stop_larm == 1)){
		DATA_GRE = 1;
		for (j=0;j<8;j++){
			if (second != second_now){
				break;
			}
			for (i=0;i<6;i++){
				for (z=0;z<8;z++){
					temp = data_base[i*8+z];
					temp <<= j;
					temp = temp & 0x80;
					if (temp == 0x80) {
						DATA_RED = 1;
					}
					else {
						DATA_RED = 0;
					}
					SH_CP_RED = 0;
					SH_CP_RED = 1;
					SH_CP_GRE = 0;
					SH_CP_GRE = 1;
					P2 = 0x00;
				}
				ST_CP_RED = 0;
				ST_CP_RED = 1;
				ST_CP_GRE = 0;
				ST_CP_GRE = 1;		 
			}
			P2 = 0x01<<j;
			for (ai=0;ai<4;ai++)
				for (aj=0;aj<5;aj++);
		}
		for (ai=0;ai<4;ai++)
			for (aj=0;aj<5;aj++);
	}
	else {
		for (j=0;j<8;j++){
			if (second != second_now){
				break;
			}
			DATA_RED = 1;
			for (i=0;i<6;i++){
				for (z=0;z<8;z++){
					temp = data_base[i*8+z];
					temp <<= j;
					temp = temp & 0x80;
					if (temp == 0x80) {
						DATA_GRE = 1;
					}
					else {
						DATA_GRE = 0;
					}
					SH_CP_RED = 0;
					SH_CP_RED = 1;
					SH_CP_GRE = 0;
					SH_CP_GRE = 1;
					P2 = 0x00;
				}
				ST_CP_GRE = 0;
				ST_CP_GRE = 1;
				ST_CP_RED = 0;
				ST_CP_RED = 1;		 
			}
			P2 = 0x01<<j;
			for (ai=0;ai<4;ai++)
				for (aj=0;aj<5;aj++);
		}
		for (ai=0;ai<4;ai++)
			for (aj=0;aj<5;aj++);
	}
}

void display_Time(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp;

	for (j=0;j<8;j++){
		if (second != second_now){
			break;
		}
		for (i=0;i<6;i++){
			for (z=0;z<8;z++){
				temp = data_base[i*8+z];
				temp <<= j;
				temp = temp & 0x80;
				if (temp == 0x80) {
					DATA_RED = 1;
				}
				else {
					DATA_RED = 0;
				}
				SH_CP_RED = 0;
				SH_CP_RED = 1;
				P2 = 0x00;
			}
			ST_CP_RED = 0;
			ST_CP_RED = 1;		 
		}

		P2 = 0x01<<j;
		for (ai=0;ai<4;ai++)
			for (aj=0;aj<5;aj++);
	}
	for (ai=0;ai<4;ai++)
		for (aj=0;aj<5;aj++);
}

void display_Start(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp;
	set_Sport();
	while ((mod == 3) && (stop == 0)){
		for (j=0;j<8;j++){
			for (i=0;i<6;i++){
				for (z=0;z<8;z++){
					temp = data_base[i*8+z];
					temp <<= j;
					temp = temp & 0x80;
					if (temp == 0x80) {
						DATA_RED = 1;
					}
					else {
						DATA_RED = 0;
					}
					SH_CP_RED = 0;
					SH_CP_RED = 1;
					P2 = 0x00;
				}
				ST_CP_RED = 0;
				ST_CP_RED = 1;		 
			}
	
			P2 = 0x01<<j;
			for (ai=0;ai<4;ai++)
				for (aj=0;aj<5;aj++);
		}
		for (ai=0;ai<4;ai++)
			for (aj=0;aj<5;aj++);
	}
}

void display_Stop(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp;
	
	if (check_stop == 0){
		m_sport = minus_sport;
		s_sport = second_sport;
		p_sport = percent_sport;
		set_Sport();
	}
	else {
		set_Sport_Mode();
	}

	while ((mod == 3) && (stop == 2)){
		for (j=0;j<8;j++){
			for (i=0;i<6;i++){
				for (z=0;z<8;z++){
					temp = data_base[i*8+z];
					temp <<= j;
					temp = temp & 0x80;
					if (temp == 0x80) {
						DATA_RED = 1;
					}
					else {
						DATA_RED = 0;
					}
					SH_CP_RED = 0;
					SH_CP_RED = 1;
					P2 = 0x00;
				}
				ST_CP_RED = 0;
				ST_CP_RED = 1;		 
			}
	
			P2 = 0x01<<j;
			for (ai=0;ai<4;ai++)
				for (aj=0;aj<5;aj++);
		}
		for (ai=0;ai<4;ai++)
			for (aj=0;aj<5;aj++);
	}
	if (mod != 3){
		check_stop = 1;
	}
	if (stop != 2){
		check_stop = 0;
	}
}

void display_Next(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp;
	
	while ((mod == 3) && (stop == 1)){
		P2 = 0x00;
		set_Sport();
		for (j=0;j<8;j++){
			for (i=0;i<6;i++){
				for (z=0;z<8;z++){
					temp = data_base[i*8+z];
					temp <<= j;
					temp = temp & 0x80;
					if (temp == 0x80) {
						DATA_RED = 1;
					}
					else {
						DATA_RED = 0;
					}
					SH_CP_RED = 0;
					SH_CP_RED = 1;
					P2 = 0x00;
				}
				ST_CP_RED = 0;
				ST_CP_RED = 1;		 
			}
	
			P2 = 0x01<<j;
			for (ai=0;ai<4;ai++)
				for (aj=0;aj<5;aj++);
		}
		for (ai=0;ai<4;ai++)
			for (aj=0;aj<5;aj++);
	}
}
void display_Date(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	for (count_up = 0; count_up < 50; count_up++){
		for (j=0;j<8;j++){
			for (i=0;i<6;i++){
				for (z=0;z<8;z++){
					temp = data_base[i*8+z];
					temp <<= j;
					temp = temp & 0x80;
					if (temp == 0x80) {
						DATA_RED = 1;
					}
					else {
						DATA_RED = 0;
					}
					SH_CP_RED = 0;
					SH_CP_RED = 1;
				}
				P2 = 0x00;
				ST_CP_RED = 0;
				ST_CP_RED = 1;			 
			}
	
			P2 = 0x01<<j;
			for (ai=0;ai<4;ai++)
				for (aj=0;aj<5;aj++);
		}		
	}
}

void display_Date_Day(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned  day_now = day;
	while ((sel == 0) && (mod == 1)){
		if (day_now != day) {
			Date();
			day_now = day;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if (i*8+z < 13){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}


void display_Date_Month(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned month_now = month;
	while ((sel == 1) && (mod == 1)){
		if (month != month_now){
			Date();
			month_now = month;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if ((i*8+z > 16)&&(i*8+z < 30)){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}


void display_Date_Year(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned int year_now = year;
	while ((sel == 2) && (mod == 1)){
		if (year_now != year){
			Date();
			year_now = year;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if ((i*8+z > 33)&&(i*8+z < 47)){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}

void display_Alert_Hour(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned  hour_alert_now = hour_alert;
	while ((sel == 0) && (mod == 2)){
		if (hour_alert_now != hour_alert) {
			larm_Update();
			hour_alert_now = hour_alert;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if (i*8+z < 13){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}

void display_Alert_Minus(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned  minus_alert_now = minus_alert;
	while ((sel == 1) && (mod == 2)){
		if (minus_alert_now != minus_alert) {
			larm_Update();
			minus_alert_now = minus_alert;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if ((i*8+z < 30) && (i*8+z > 16)){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}

void display_Alert_Choice(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned  set_larm_now = set_larm;
	while ((sel == 2) && (mod == 2)){
		if (set_larm_now != set_larm) {
			larm_Update();
			set_larm_now = set_larm;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if ((i*8+z < 47) && (i*8+z >33)){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}



void display_Time_Hour(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned  set_hour_now = set_hour;
	while ((sel == 0) && (mod == 4)){
		if (set_hour_now != set_hour) {
			set_Time_Update();
			set_hour_now = set_hour;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if (i*8+z < 13){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}

void display_Time_Minus(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned  set_minus_now = set_minus;
	while ((sel == 1) && (mod == 4)){
		if (set_minus_now != set_minus) {
			set_Time_Update();
			set_minus_now = set_minus;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if ((i*8+z < 30) && (i*8+z > 16)){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}

void display_Time_Choice(){
	unsigned int ai,aj;
	unsigned char i,j,z,temp,count_up;
	unsigned  set_time_ok_now = set_time_ok;
	while ((sel == 2) && (mod == 4)){
		if (set_time_ok_now != set_time_ok) {
			set_Time_Update();
			set_time_ok_now = set_time_ok;
		}
		for (count_up = 0; count_up < 15; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						if ((i*8+z < 47) && (i*8+z >33)){
							temp = 0xff;
						}
						else {
							temp = data_base[i*8+z];
						}
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}
		}
		for (count_up = 0; count_up < 25; count_up++){
			for (j=0;j<8;j++){
				for (i=0;i<6;i++){
					for (z=0;z<8;z++){
						temp = data_base[i*8+z];
						temp <<= j;
						temp = temp & 0x80;
						if (temp == 0x80) {
							DATA_RED = 1;
						}
						else {
							DATA_RED = 0;
						}
						SH_CP_RED = 0;
						SH_CP_RED = 1;
					}
					P2 = 0x00;
					ST_CP_RED = 0;
					ST_CP_RED = 1;			 
				}
		
				P2 = 0x01<<j;
				for (ai=0;ai<4;ai++)
					for (aj=0;aj<5;aj++);
			}		
		}
	}
}

void set_Time_Alert(){
	unsigned char i,z, col, count = 0;
	for (i = 0;i<6;i++){
		for (z=0;z<8;z++){
			col = i*8+z;
			if (col <= 6){
				data_base[col] = DATA_OFFSET[(am_pm == 1 ? (hour_alert > 12 ? hour_alert - 12: hour_alert): hour_alert)/10*7+count];
				count++; 
			}
			else if (col <= 13){
				data_base[col] = DATA_OFFSET[((am_pm == 1 ? (hour_alert > 12 ? hour_alert - 12: hour_alert): hour_alert)%10)*7+count];
				count++; 
			}
			else if (col <= 16){
				data_base[col] = DATA_OFFSET[(am_pm == 1 ? (hour_alert > 12 ? 124: 121): 70)+count];
				count++; 
			}
			else if (col <= 23){
				data_base[col] = DATA_OFFSET[(minus_alert/10)*7+count];
				count++; 
			}
			else if (col <= 30){
				data_base[col] = DATA_OFFSET[(minus_alert%10)*7+count];
				count++;
			}
			else if (col <= 33){
				data_base[col] = 0xff;
				count++;
			}
			else if (col <= 40){
				data_base[col] = DATA_OFFSET[((set_larm == 0) ? 114 : 100)+count];
				count++;
			}
			else if (col <= 47){
				data_base[col] = DATA_OFFSET[((set_larm == 0) ?  100: 107)+count];
				count++;
			}
			
			if (col == 6 || col == 13 || col == 16 || col == 23 || col == 30 || col == 33 || col == 40 || col == 47){
				count = 0;
			}
		}		  //  1 matrix.
	}
}

void set_Time(){
	unsigned char i,z, col, count = 0;
	for (i = 0;i<6;i++){
		for (z=0;z<8;z++){
			col = i*8+z;
			if (col <= 6){
				data_base[col] = DATA_OFFSET[((am_pm == 1 ? (set_hour > 12 ? set_hour - 12: set_hour): set_hour)/10)*7+count];
				count++; 
			}
			else if (col <= 13){
				data_base[col] = DATA_OFFSET[((am_pm == 1 ? (set_hour > 12 ? set_hour - 12: set_hour): set_hour)%10)*7+count];
				count++; 
			}
			else if (col <= 16){
				data_base[col] = DATA_OFFSET[(am_pm == 1 ? (set_hour > 12 ? 124: 121): 70)+count];
				count++; 
			}
			else if (col <= 23){
				data_base[col] = DATA_OFFSET[(set_minus/10)*7+count];
				count++; 
			}
			else if (col <= 30){
				data_base[col] = DATA_OFFSET[(set_minus%10)*7+count];
				count++;
			}
			else if (col <= 33){
				data_base[col] = 0xff;
				count++;
			}
			else if (col <= 40){
				data_base[col] = DATA_OFFSET[((set_time_ok == 0) ? 114 : 100)+count];
				count++;
			}
			else if (col <= 47){
				data_base[col] = DATA_OFFSET[((set_time_ok == 0) ?  100: 107)+count];
				count++;
			}
			
			if (col == 6 || col == 13 || col == 16 || col == 23 || col == 30 || col == 33 || col == 40 || col == 47){
				count = 0;
			}
		}		  //  1 matrix.
	}
}


void set_Day(){
	unsigned char i,z, col, count = 0;
	for (i = 0;i<6;i++){
		for (z=0;z<8;z++){
			col = i*8+z;
			if (col <= 6){
				data_base[col] = DATA_OFFSET[(day/10)*7+count];
				count++; 	
			}
			else if (col <= 13){
				data_base[col] = DATA_OFFSET[(day%10)*7+count];
				count++; 
			}
			else if (col <= 16){
				data_base[col] = DATA_OFFSET[73+count];
				count++; 
			}
			else if (col <= 23){
				data_base[col] = DATA_OFFSET[(month/10)*7+count];
				count++; 
			}
			else if (col <= 30){
				data_base[col] = DATA_OFFSET[(month%10)*7+count];
				count++; 
			}
			else if (col <= 33){
				data_base[col] = DATA_OFFSET[73+count];
				count++;
			}
			else if (col <= 40){
				data_base[col] = DATA_OFFSET[(year%100)/10*7+count];
				count++;
			}
			else if (col <= 47){
				data_base[col] = DATA_OFFSET[(year%10)*7+count];
				count++;
			}
			
			if (col == 6 || col == 13 || col == 16 || col == 23 || col == 30 || col == 33 || col == 40 || col == 47){
				count = 0;
			}
		}		  //  1 matrix.
	}
}




void Dem_1s(void) interrupt 1 
{
    TH0=0xB8; //TH0*256+TL0=65536-18432
 	TL0=0x01;
    count_over = count_over + 1;
 	if (stop != 0){
		percent_sport = percent_sport + 2;
		if (percent_sport == 100){
			percent_sport = 0;
			second_sport++;
			if (second_sport == 60){
				second_sport = 0;
				minus_sport++;
				if (minus_sport == 100){
					minus_sport = 0;
				}
			} 
		}
	}

    if(count_over == 50)// dung 1s
    {
		count_over = 0;
		second++;	
 		if (second == 60) {
			second = 0;
			minus++;
			if (minus == 60) {
				minus = 0;
				hour++;
				if (hour == 24) {
					hour = 0;
					day++;
					if ((day == 29 && month == 2)||(day == 31 && (month == 4 || month == 6 || month == 9 || month == 11))||(day == 32 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))){ 
						day = 1;
						month++;
						if (month == 13){
							month = 1;
							year += 1;
						}
					}
				}
			}
		}
    }
}


void get_Key(void) interrupt 3
{
	unsigned char aj,ai;
	count_over_1++;
	TH1 = 0x00;
	TL1 = 0x00;
	if (count_over_1 == 5){
		count_over_1 = 0;
		if (MODE == 1) {
			for (ai=0;ai<40;ai++)
				for (aj=0;aj<100;aj++);
			mod++;
			if (MODE == 1){
				sel = 0;
				if (mod == 5){
					mod = 0;
				}
	 		}
		}
		if (SELECT == 1) {
			for (ai=0;ai<30;ai++)
				for (aj=0;aj<100;aj++);
			if (SELECT == 1){
				sel++;
				if (mod == 0){
					if ((set_larm == 1) && (hour == hour_alert) && (minus == minus_alert) && (stop_larm == 0)){
						stop_larm = 1;
					}
					else if (am_pm == 1){
						am_pm = 0;
					}
					else {
						am_pm = 1;
					}
				}
				if ((mod == 1) || (mod == 2) || (mod == 4)){
					if (sel == 3){
						sel = 0;
					}
				}
				if (mod == 3){
					stop++;
					if (stop == 3) {
						stop = 1;
					}
				}
			}
		}
	
		if (CHANGE == 1) {
			for (ai=0;ai<40;ai++)
				for (aj=0;aj<100;aj++);
			if (CHANGE == 1){
				if (mod == 0){
					check_day = 1;		
				}
				else if (mod == 1){
					if (sel == 0) {
						day++;
						if ((day >= 29 && month == 2)||(day >= 31 && (month == 4 || month == 6 || month == 9 || month == 11))||(day >= 32 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))){ 
							day = 1;
						}
					}
					if (sel == 1) {
						month++;
						if (month >=13){
							month = 1;
						}
					}
					if (sel == 2) {
						year++;
						if (year >=2100){
							year = 2000;
						}
					}					
				}
				else if (mod == 2) {
					if (sel == 0) {
						hour_alert++;
						if (hour_alert >=24){
							hour_alert = 0;
						}		
					}
					if (sel == 1) {
						minus_alert++;
						if (minus_alert >= 60){
							minus_alert = 0;
						}
					}
					if (sel == 2) {
						if (set_larm == 0){
							set_larm = 1;
						}
						else
						{
							set_larm = 0;
						}	
					}	
				}
				else if (mod == 3) {
					stop = 0;
					minus_sport = 0;
					second_sport = 0;
					percent_sport = 0;
				}
				else if (mod == 4) {
					if (sel == 0) {
						set_hour++;
						if (set_hour >=24){
							set_hour = 0;
						}		
					}
					if (sel == 1) {
						set_minus++;
						if (set_minus >= 60){
							set_minus = 0;
						}
					}
					if (sel == 2) {
						if (set_time_ok == 0){
							set_time_ok = 1;
						}
						else
						{
							set_time_ok = 0;
						}	
					}	
				}
			}
		}
	}	
}		

void main(){
	//Cai dat thong so ban dau
	unsigned char i,z,count=0,col;
	
	P2 = 0x00;
	MAIN_RESET_RED = 1;	
	MAIN_RESET_GRE = 1;			   // disable green led with 64bit - 8 ic 74hc595
	DATA_GRE = 1;
	for (i=0;i<8;i++){
		for (z=0;z<8;z++){	
			SH_CP_GRE = 0;
			SH_CP_GRE = 1;
		}
		ST_CP_GRE = 0;
		ST_CP_GRE = 1;				   //latch 64bit to matrix
	}


	for (i = 0;i<6;i++){			  //set first matrix dislay
		for (z=0;z<8;z++){
			col = i*8+z;
			if (col <= 6){
				data_base[col] = DATA_OFFSET[((hour > 12 ? (hour-12):hour)/10)*7+count];
				count++; 	
			}
			else if (col <= 13){
				data_base[col] = DATA_OFFSET[((hour > 12 ? (hour-12):hour)%10)*7+count];
				count++; 
			}
			else if (col <= 16){
				data_base[col] = DATA_OFFSET[70+count];
				count++; 
			}							 
			else if (col <= 23){
				data_base[col] = DATA_OFFSET[(minus/10)*7+count];
				count++; 
			}
			else if (col <= 30){
				data_base[col] = DATA_OFFSET[(minus%10)*7+count];
				count++; 
			}
			else if (col <= 33){
				data_base[col] = DATA_OFFSET[70+count];
				count++;
			}
			else if (col <= 40){
				data_base[col] = DATA_OFFSET[(second/10)*7+count];
				count++;
			}
			else if (col <= 47){
				data_base[col] = DATA_OFFSET[(second%10)*7+count];
				count++;
			}
			/*else {									 //if we use 6 matrix we will not need this
				if (hour < 12){
					data_base[col] = DATA_OFFSET[76+count];
					count++;
				}
				else {
					data_base[col] = DATA_OFFSET[84+count];
					count++;
				}
			} */
			if (col == 6 || col == 13 || col == 16 || col == 23 || col == 30 || col == 33 || col == 40 || col == 47){
				count = 0;
			}
		}		  
	}			  //set first matrix
	
	
	TMOD = 0x11;  //enable timer 0, timer 1
	EA = 1;
	ET0 = 1;
	ET1 = 1;
	TH0 = 0xB8;			 //set timer0 = 0x100F	for real time
	TL0 = 0x01;
	TH1 = 0x00;
	TL1 = 0x00;			 //set timer1 = 0xB2F0 for get_key
	TR1  = 1;
	TR0 = 1;
	

	while (1) {
		switch (mod){		   //choice Mode
			case 0:					//Mode 0 : display time
				P2 = 0x00;
				second_now = second;
				update_Matrix();
				if ((set_larm == 1) && (hour == hour_alert) && (minus == minus_alert)){
					for (i=0;i<50;i++){
						display_Time_Alarm();
						P2 = 0x00;	
						if (second != second_now || mod != 0){
							break;
						}
					}
					if (second == 0){
						stop_larm = 0;
					}
				}
				else {
					for (i=0;i<50;i++){
						display_Time();
						P2 = 0x00;	
						if (second != second_now || mod != 0){
							break;
						}
					}
				}
				if (check_day == 1) {
					set_Day();
					display_Date();
					check_day = 0;
				}
				break;
			case 1:					//Mode 1 : display and edit Date
				set_Day();
				switch (sel) {
					case 0:
						display_Date_Day();
						break;
					case 1:
						display_Date_Month();
						break;
					default:
						display_Date_Year();
						break;
				}
				P2 = 0x00;
				break;
			case 2:					 //Mode 2: set time for Alarm
				set_Time_Alert();
				switch (sel){
					case 0:
						display_Alert_Hour();
						break;
					case 1:
						display_Alert_Minus();
						break;
					default:
						display_Alert_Choice();
						break;
				}
				P2 = 0x00;
				break;
			case 3:					 //Mode 3: sport clock
				if (stop == 0){
					display_Start();
				}
				else if (stop == 2){
					display_Stop();
				}
				else {
					display_Next();
				}
				P2 = 0x00;
				break;
			default:
				if (check_out_time == 0){
					set_hour = hour;
					set_minus = minus;
				}
				set_Time();
				switch (sel) {
					case 0:
						display_Time_Hour();
						break;
					case 1:
						display_Time_Minus();
						break;
					default:
						display_Time_Choice();
						break;
				}
				if (mod == 4) {
					check_out_time = 1;
				}
				else {
					check_out_time = 0;
					if (set_time_ok == 1){
						hour = set_hour;
						minus = set_minus;
						second = 0;
						set_time_ok = 0;
					}
				}

				P2 = 0x00;				 //Mode 4: set time
				break;
		}
	}
}